package summary;

import java.util.PriorityQueue;

/**
 * @Author: 海琳琦
 * @Date: 2022/6/8 22:28
 * https://leetcode.cn/problems/last-stone-weight/
 */
public class Title1046 {

    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> (b.compareTo(a)));
        for (int i = 0; i < stones.length; i++) {
            queue.offer(stones[i]);
        }
        while (queue.size() >= 2) {
            Integer b1 = queue.poll();
            Integer b2 = queue.poll();
            int gap = b1 - b2;
            if (gap != 0) {
                queue.offer(gap);
            }
        }
        return queue.isEmpty() ? 0 : queue.peek();
    }

    public static void main(String[] args) {

    }
}
